package org.pps.gl.client.vue;

import org.pps.gl.client.GetInfoService;
import org.pps.gl.client.GetInfoServiceAsync;
import org.pps.gl.client.Pps_gl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;

/**
 * Affichage des études : période, école, spécialité
 * 
 */
public class PFL_Etude extends Composite {

	private long idEtude;
	private Profil profil;

	public PFL_Etude(String periode, String nomEcole, String specialite,
			long id, Profil p) {

		this.idEtude = id;
		this.profil = p;

		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setStyleName("profilMarge");
		initWidget(verticalPanel);
		verticalPanel.setSize("400px", "80px");

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setWidth("100%");

		Label lblNomEcole = new Label(nomEcole);
		horizontalPanel.add(lblNomEcole);
		lblNomEcole.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		lblNomEcole.setStylePrimaryName("dynTitre");
		lblNomEcole.setStyleName("dynTitre");
		verticalPanel.setCellVerticalAlignment(lblNomEcole,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblNomEcole.setSize("100%", "30px");
		verticalPanel.setCellHorizontalAlignment(lblNomEcole,
				HasHorizontalAlignment.ALIGN_CENTER);

		if (p != null) {
			Button sup = new Button("");
			horizontalPanel.add(sup);
			horizontalPanel.setCellHorizontalAlignment(sup,
					HasHorizontalAlignment.ALIGN_RIGHT);
			horizontalPanel.setCellVerticalAlignment(sup,
					HasVerticalAlignment.ALIGN_MIDDLE);
			sup.setStylePrimaryName("boutonSuppression");
			sup.setSize("21px", "21px");
			sup.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					GetInfoServiceAsync infoService = GWT
							.create(GetInfoService.class);
					infoService.deleteEtude(idEtude,
							new AsyncCallback<Boolean>() {

								@Override
								public void onSuccess(Boolean result) {
									// On rafraîchie les données sur la page
									Pps_gl.getCm().refresh();
									profil.afficherEtudes();
									GWT.log("Experience effacée.");
								}

								@Override
								public void onFailure(Throwable caught) {
									GWT.log("Erreur d'effacement d'expérience.");
								}
							});
				}
			});
		}
		Label lblPeriode = new Label(periode);
		lblPeriode.setStyleName("dynDate");
		lblPeriode.setStylePrimaryName("dynDate");
		verticalPanel.add(lblPeriode);
		verticalPanel.setCellVerticalAlignment(lblPeriode,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblPeriode.setSize("100%", "30px");
		verticalPanel.setCellHorizontalAlignment(lblPeriode,
				HasHorizontalAlignment.ALIGN_CENTER);

		Label lblSpecialite = new Label("Spécialité : " + specialite);
		verticalPanel.add(lblSpecialite);
		lblSpecialite.setStyleName("dynTexte");
		lblSpecialite.setStylePrimaryName("dynTexte");
		verticalPanel.setCellVerticalAlignment(lblSpecialite,
				HasVerticalAlignment.ALIGN_MIDDLE);
		lblSpecialite.setSize("80%", "30px");
		verticalPanel.setCellHorizontalAlignment(lblSpecialite,
				HasHorizontalAlignment.ALIGN_CENTER);
	}
}
